﻿using System;
using System.Collections.Generic;
using System.Text;
using PluginInterface;
using System.IO;

namespace telstar
{
    public class EpgPlugin : IEpgPlugin
    {
        private string name = "telstar";
        private string comment = "";
        private string author = "MulticastTV";
        private string version = "0.1";
        private string multicasttvVersion = "0.30.0.0";
        private string lockedToLang = "sl-si";
        private string type = "online";

        // default regex settings
        private string hostname = "http://www.sibila.si/";
        private string httpMethod = "GET";
        private string initRequest = "";
        private string initHttpMethod = "GET";
        private bool initRequestNeeded = false;  // we need to get "flag" from main page first

        IPluginHost myHost = null;

        public IPluginHost Host
        {
            get { return myHost; }
            set { myHost = value; }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public EpgPlugin()
        {

        }

        public string Author
        {
            get { return author; }
        }

        public string Version
        {
            get { return version; }
        }

        public string MulticasttvVersion
        {
            get { return multicasttvVersion; }
        }

        public string Comment
        {
            get { return comment; }
        }

        public string Name
        {
            get { return name; }
        }

        public string PluginType
        {
            get { return type; }
        }

        public string LockedToUserInterfaceLangue
        {
            get { return lockedToLang; }
        }

        public string HostName
        {
            get { return hostname; }
        }

        public string RequestMethod
        {
            get { return httpMethod; }
        }

        public string InitialRequest
        {
            get { return initRequest; }
        }

        public bool InitialRequestNeeded
        {
            get { return initRequestNeeded; }
            set { initRequestNeeded = value; }
        }

        public string InitialRequestMethod
        {
            get { return initHttpMethod; }
        }


        public void Initialize(string Path)
        {

        }

        public void Dispose()
        {

        }

        public string[] GetTodayRequest(string ChannelId)
        {
            string request = "";
            string referer = "";
            string postString = "";

            request = "telstar/spored/index.php?akc=iz_kan&id="+ChannelId;
            referer = hostname + "telstar/spored/index.php";


            string[] RequestParams = {request,    // http request
                                      referer ,    // http referer
                                      postString  // POST string
                                     };
            return RequestParams;
        }

        public string[] GetTodaySunmmaryRequest()
        {
            string[] RequestParams = { };
            return RequestParams;

        }

        public void ParseInitPage(string Html)
        {

        }

        public List<string[]> ParseToday(string Html)
        {

            List<string[]> Plist = new List<string[]>();
            Plist.Clear();

           //<td width="6%">06:00</td>
           //<td><a onMouseover="fixedtooltip('Nadaljevanka in nanizanka', this, event)" onMouseout="delayhidetip()" href="javascript:NovoOkno('opisi.php?dan=8.10.09&id=392&tv=Hallmark')">Angelski dotik III (Touched by an Angel III), am. serija, 1996</a></td>
            string table_Start = @"<table border=""0"" cellspacing=""0"" cellpadding=""2"" align=""center"" width=""100%"">";
            string table_Stop = @"</table";
            
            int start = Html.IndexOf(table_Start);
            int stop =-1;
            if (start != -1)
            {
                 stop = Html.IndexOf(table_Stop,start);
            }

            //našli smo tabelo
            if (stop != -1 && start != -1)
            {
               string tabela = Html.Substring(start, stop - start);
               int  row1_start = tabela.IndexOf(@"<td width=""6");
               int row1_stop = 0;
               while (row1_start != -1)
               {
                   int row1_ura_start = tabela.IndexOf(">",row1_start);
                   row1_stop = tabela.IndexOf("</td>",row1_start);
                   if (row1_stop==-1)
                       break;
                   
                   // ura
                   row1_start = row1_ura_start + 1;
                   string ura = tabela.Substring(row1_start, row1_stop - row1_start);

                   //opis in link(opcijsko)
                   int row2_start = row1_stop+5;
                   int row2_stop = tabela.IndexOf("</td>",row2_start);
                   if (row2_stop == -1)
                       break;

                   //poglejmo ali imamo opis ali pa je brez.
                   int test1 = tabela.IndexOf("<td><a onMouseover",row2_start,row2_stop-row2_start);
                   int test2 = tabela.IndexOf("<td onMouseover", row2_start, row2_stop - row2_start);

                   if (test1 == -1 && test2 == -1)
                       break;

                   string link = "0";  // empty link must be "0" !!!
                   string naslov = "";
                   string tip = "";

                   // tip oddaje
                   int tip_start = tabela.IndexOf("fixedtooltip(", row2_start);
                   int tip_stop = tabela.IndexOf("',",Math.Max(0,tip_start));
                   if (tip_start != -1 && tip_stop != -1 && tip_stop > tip_start)
                   {
                       tip_start = tip_start + 14;
                       tip = tabela.Substring(tip_start, tip_stop - tip_start);
                   }

                   if (test1 != -1)
                   { 
                       // imamo link
                       int link_start = tabela.IndexOf("opisi.php",row2_start);
                       int link_stop = tabela.IndexOf("')",Math.Max(0,link_start));
                       int naslov_start = tabela.IndexOf(">", Math.Max(0,link_stop));
                       int naslov_stop = tabela.IndexOf("<",Math.Max(0,naslov_start));

                       if (link_start != -1 && link_stop != -1 && link_stop>link_start)
                       {
                           link ="http://www.sibila.si/telstar/spored/" + tabela.Substring(link_start, link_stop - link_start);
                       }
                       if (naslov_start != -1 && naslov_stop != -1 && naslov_stop > naslov_start)
                       {
                           naslov_start = naslov_start + 1;
                           naslov =tabela.Substring(naslov_start, naslov_stop - naslov_start);
                       }
                   }
                   else if (test2 != -1)
                   { 
                       // nimamo linka
                       int naslov_start = tabela.IndexOf(">", Math.Max(0, row2_start));
                       int naslov_stop = tabela.IndexOf("<", Math.Max(0, naslov_start));
                       if (naslov_start != -1 && naslov_stop != -1 && naslov_stop > naslov_start)
                       {
                           naslov_start = naslov_start + 1;
                           naslov = tabela.Substring(naslov_start, naslov_stop - naslov_start);
                       }
                   }

                   row1_start = tabela.IndexOf(@"<td width=""6",row1_stop);

                   ura = ura + " - ";
                   string[] data = new string[4];
                   data[0] = ura;    // time  "hh:mm - "  
                   data[1] = naslov; // title
                   data[2] = tip;    // genre
                   data[3] = link;   // details link

                   Plist.Add(data);
               }
            
            }
            return Plist;
        }

        public string[] ParseDetails(string Html)
        {
            string title="";
            string genre="";
            string description="";
            string channelName="";
            string time="";
            string actors="";
            string actorsTitle="";
            string director="";
            string directorTitle="";
            string detailsLink = "";

            string ura = "";
            string datum = "";
            int channel_start = Html.IndexOf("<td>");
            if (channel_start != -1)
            {
                channel_start = channel_start + 4;
                int channel_stop = Html.IndexOf("</td>", channel_start);
                if (channel_stop != -1)
                {
                    channelName = Html.Substring(channel_start, channel_stop - channel_start);
                    int time_start = Html.IndexOf("<td>",channel_stop);
                    if (time_start != -1)
                    {
                         time_start = time_start + 4;
                         int time_stop = Html.IndexOf("</td>", time_start);
                         if (time_stop != -1)
                         {
                             ura = Html.Substring(time_start, time_stop - time_start);

                             int datum_start = Html.IndexOf(">", time_stop);
                             if (datum_start != -1)
                             {
                                 datum_start=datum_start+1;
                                 int datum_stop = Html.IndexOf("<", datum_start);
                                 if (datum_stop != -1)
                                     datum = Html.Substring(datum_start, datum_stop - datum_start);
                             }
                         }
                    }
                }
            }
            time = datum + " " + ura;

            string naslovmix = "";
            int naslov_start = Html.IndexOf(@"class=""bold"">");
            if (naslov_start != -1)
            {
                naslov_start = naslov_start + 13;
                int naslov_stop = Html.IndexOf("</td>", naslov_start);
                if (naslov_stop != -1 && naslov_stop>naslov_start)
                    naslovmix = Html.Substring(naslov_start, naslov_stop - naslov_start);
            }

            if (naslovmix.Contains(","))
            {
                string[] mix = naslovmix.Split(",".ToCharArray(), 2);
                title = mix[0];
                genre = mix[1];
            }
            else
                title = naslovmix;

            string naslovVsebinamix = "";
            int naslov2_start = Html.IndexOf(@"class=""op"">");
            if (naslov2_start != -1)
            {
                naslov2_start = naslov2_start + 11;
                int naslov2_stop = Html.IndexOf("</td>", naslov2_start);
                if (naslov2_stop != -1 && naslov2_stop > naslov2_start)
                    naslovVsebinamix = Html.Substring(naslov2_start, naslov2_stop - naslov2_start);
            
                if (naslovVsebinamix.Contains("</br></br>"))
                {
                    string sep = "</br></br>";
                    int sepIndex = Html.IndexOf(sep);
                    title = Html.Substring(naslov2_start, sepIndex-naslov2_start);
                    description = Html.Substring(sepIndex + 10, naslov2_stop - (sepIndex+10));
                }
                else
                    description = naslovVsebinamix;
            }

            title =title.Replace("</br>", "\n");
            description = description.Replace("</br>", "\n");
            title = title.Replace("<br>", "\n");
            description = description.Replace("<br>","\n");


            return new string[] { title, genre, description, channelName, time, actors, actorsTitle, director, directorTitle, detailsLink };
        }


        public List<string[]> ParseTodaySummary(string Html)
        {
            List<string[]> Plist = new List<string[]>();
            Plist.Clear();
            return Plist;
        }

    }
}
